package cn.wolfcode.web.controller;

import cn.wolfcode.common.web.Result;
import cn.wolfcode.config.AlipayConfig;
import cn.wolfcode.config.AlipayProperties;
import cn.wolfcode.domain.PayVo;
import cn.wolfcode.domain.RefundVo;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.internal.util.AlipaySignature;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.request.AlipayTradeRefundRequest;
import com.alipay.api.response.AlipayTradeRefundResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;


@RestController
@RequestMapping("/alipay")
public class AlipayController {
    @Autowired
    private AlipayClient alipayClient;
    @Autowired
    private AlipayProperties alipayProperties;

    /**
     * 返回支付页面需要的表单字符串
     * @param payVo
     * @return
     * @throws AlipayApiException
     */
    @RequestMapping("/pay")
    public Result<String> pay(@RequestBody PayVo payVo) throws AlipayApiException {
        //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(payVo.getReturnUrl());
        alipayRequest.setNotifyUrl(payVo.getNotifyUrl());

        alipayRequest.setBizContent("{\"out_trade_no\":\"" + payVo.getOutTradeNo() + "\","
                + "\"total_amount\":\"" + payVo.getTotalAmount() + "\","
                + "\"subject\":\"" + payVo.getSubject() + "\","
                + "\"body\":\"" + payVo.getBody() + "\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        //请求
        String result = alipayClient.pageExecute(alipayRequest).getBody();

        return Result.success(result);
    }

    /**
     * 延签操作
     * @param params
     * @return
     * @throws AlipayApiException
     */
    @RequestMapping("/rsaCheckV1")
    Result<Boolean> rsaCheckV1(@RequestBody Map<String, String> params) throws AlipayApiException {
        boolean signVerified = AlipaySignature.rsaCheckV1(params, alipayProperties.getAlipayPublicKey(),
                alipayProperties.getCharset(), alipayProperties.getSignType()); //调用SDK验证签名
        return Result.success(signVerified);
    }

    /**
     * 退款操作
     * @param refundVo
     * @return
     * @throws AlipayApiException
     */
    @RequestMapping("/refund")
    Result<Boolean> refund(@RequestBody RefundVo refundVo) throws AlipayApiException {
        //设置请求参数
        AlipayTradeRefundRequest alipayRequest = new AlipayTradeRefundRequest();
        alipayRequest.setBizContent("{\"out_trade_no\":\""+ refundVo.getOutTradeNo() +"\","
                + "\"trade_no\":\""+ "" +"\","
                + "\"refund_amount\":\""+ refundVo.getRefundAmount() +"\","
                + "\"refund_reason\":\""+ refundVo.getRefundReason() +"\","
                + "\"out_request_no\":\""+ "" +"\"}");

        //请求
        AlipayTradeRefundResponse execute = alipayClient.execute(alipayRequest);
        return Result.success(execute.isSuccess());
    }
}
